﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Tab;
// 邵家晨完成
namespace BirthdayPlan
{
    public partial class Input1 : Form
    {
        public Input1()
        {
            InitializeComponent();
            labprompt.Visible = false;
        }

        //点击进行计算，提示跳转
        private void subbtn_Click(object sender, EventArgs e)
        {
            int res = 0;
            string input1 = textBox1.Text; //出生日期 xxxx-mm-nn
            string input2 = textBox2.Text; //今天日期

            bool isValid1 = CheckDateFormat(input1);
            bool isValid2 = CheckDateFormat(input2);

            // 解析字符串为 DateTime
            DateTime birthDate = DateTime.Parse(input1);
            DateTime today = DateTime.Parse(input2);

            // 确保出生日期不大于今天
            if (birthDate > today)
            {
                MessageBox.Show("出生日期不能大于今天，请重新输入！", "输入错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textBox1.Clear(); // 清空输入框
                return; // 退出方法，避免后续计算
            }

            if (isValid1 && isValid2)
            {
                res = Caculator();
                labres.Text = "距离生日还有：" + res.ToString() + "天";
                labprompt.Visible = true;
                DataContainer.Data1 = res;
                DataContainer.Birthday = input1; //存储出生日期
                DataContainer.Today = input2;    //存储今天日期

                this.KeyDown += new KeyEventHandler(Input1_KeyDown);
            }
            else
            {
                MessageBox.Show("输入格式错误，请使用 YYYY-MM-DD 格式！");
            }
        }

        //检查输入格式
        private bool CheckDateFormat(string input)
        {
            string format = "yyyy-MM-dd";
            DateTime result;
            bool isValid = DateTime.TryParseExact(input, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out result);

            // 检查日期是否有效
            if (isValid)
            {
                int year = result.Year;
                int month = result.Month;
                int day = result.Day;

                if (month < 1 || month > 12 || day < 1 || day > DateTime.DaysInMonth(year, month))
                {
                    return false;
                }
            }

            return isValid;
        }

        

        //计算还有几天生日
        private int Caculator()
        {
            int result = 0;
            string birthdayInput = textBox1.Text;
            string todayInput = textBox2.Text;

            DateTime birthday = DateTime.ParseExact(birthdayInput, "yyyy-MM-dd", CultureInfo.InvariantCulture);
            DateTime today = DateTime.ParseExact(todayInput, "yyyy-MM-dd", CultureInfo.InvariantCulture);
            result = CalculateDaysUntilNextBirthday(birthday, today);

            return result;
        }

        //private int CalculateDaysUntilNextBirthday(DateTime birthday, DateTime today)
        //{
        //    // 计算下一次生日的日期
        //    DateTime nextBirthday = birthday.AddYears(today.Year - birthday.Year);
        //    if (nextBirthday < today)
        //    {
        //        nextBirthday = nextBirthday.AddYears(1); // 如果今年生日已过，计算明年的生日
        //    }

        //    // 计算天数差
        //    TimeSpan difference = nextBirthday - today;
        //    return difference.Days;
        //}
        private int CalculateDaysUntilNextBirthday(DateTime birthday, DateTime today)
        {
            // 郭嘉欣修改
            // 初始年份
            int currentYear = today.Year;
            DateTime nextBirthday = birthday.AddYears(currentYear - birthday.Year); // 假设生日是今年

            // 如果生日是2月29日，且今年不是闰年，找到下一个闰年
            if (birthday.Month == 2 && birthday.Day == 29)
            {
                // 如果今年不是闰年，则计算下一个闰年
                while (!DateTime.IsLeapYear(nextBirthday.Year))
                {
                    nextBirthday = nextBirthday.AddYears(1);
                }
            }

            // 如果今年的生日已经过去，则计算明年的生日
            if (nextBirthday < today)
            {
                nextBirthday = nextBirthday.AddYears(1);
            }

            // 计算天数差
            TimeSpan difference = nextBirthday - today;
            return difference.Days;
        }


        private void Input1_KeyDown(object sender, KeyEventArgs e)
        {
            Input2 form2 = new Input2();
            form2.Show();
            this.Hide();
        }
    }
}
